


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>树形组件</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiAdmin.std/src/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiAdmin.std/src/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>树形组件</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">

        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">

                    <div class="layui-form">

                        <input type="hidden" id="id" name="id" value="{$role.id}" />
                        <input type="hidden" id="menu_ids" name="menu_ids" value="{$role.menu_ids}" />
                        <input type="hidden" id="dept_ids" name="dept_ids" value="{$role.dept_ids}" />

                        <div class="layui-form-item">
                            <label class="layui-form-label">角色名称：</label>
                            <div class="layui-input-block">
                                <input type="text" id="title" name="title" value="{$role.title}"
                                       lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单权限：</label>
                            <div class="layui-input-block">

                                <input type="text" id="menu_titles" name="menu_titles" readonly
                                       lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">部门权限：</label>
                            <div class="layui-input-block">

                                <input type="text" id="dept_titles" name="dept_titles" readonly
                                       lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="btn_save" id="btn_save">保存编辑</button>
<!--                                <input type="button" class="layui-btn" value="保存编辑" onclick="save_role()" id="btn_save">-->
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layuiAdmin.std/src/layuiadmin/layui/layui.js"></script>
<script>
    var tree;
    var layer;
    var util;
    var select;
    var $;
    var form;

    //模拟数据
    var tree_data = []
    var tree_data2 = []

    function save(field) {
        $('#btn_save').hide()
        $.ajax({
            url: 'save_role',
            type: 'POST',
            data: field,
            dataType: 'json',
            success: function (data) {
                if (data.code == '0') {
                    layer.msg('保存成功', { icon: 1, time: 2000 }, function () {
                        //parent.location.reload()
                        parent.layer.closeAll()
                        parent.$('#btnSearch').click();

                        $('#btn_save').show()
                    });

                } else {
                    layer.msg(data.msg, { icon: 5, time: 2000 });

                    $('#btn_save').show()
                }
            }
        });
    }



    function render_tree_menu() {


        //树形菜单
        select.render({
            el: 'menu_titles',
            data: tree_data,
            //type: 'radio',
            checkbox: true,
            value: '{$role.menu_titles}',
            checked: function (obj) {
                console.log(JSON.stringify(obj))
                //obj.value 已选数据的title
                //obj.data 已选数据

                //alert(obj.obj.data.id)
                //alert(JSON.stringify(obj))

                //$('#pid').val(obj.obj.data.id)

                var ids = obj.combData.idents.join(',');
                var titles = obj.combData.values.join(',');

                $('#menu_ids').val(ids);
                $('#menu_titles').val(titles);
            }
        })


    }

    function render_tree_dept() {


        select.render({
            el: 'dept_titles',
            data: tree_data2,
            //type: 'radio',
            checkbox: true,
            value: '{$role.dept_titles}',
            checked: function (obj) {
                console.log(JSON.stringify(obj))

                var ids = obj.combData.idents.join(',');
                var titles = obj.combData.values.join(',');

                $('#dept_ids').val(ids);
                $('#dept_titles').val(titles);
            }
        })


    }

    function load_tree_data() {
        $.ajax({
            url: 'load_menu?role_id={$role.id}',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                if (data.code == '0') {
                    tree_data = data.tree
                    render_tree_menu()

                    //$('#menu_titles').val('{$role.menu_titles}')
                } else {
                    layer.msg(data.msg, { icon: 5, time: 2000 });
                }
            }
        });

        $.ajax({
            url: 'load_dept?role_id={$role.id}',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                if (data.code == '0') {
                    tree_data2 = data.tree
                    render_tree_dept()

                } else {
                    layer.msg(data.msg, { icon: 5, time: 2000 });
                }
            }
        });
    }

    layui.config({
        base: '/static/layuiAdmin.std/src/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tree', 'util', 'customSelect'], function(){
        tree = layui.tree
        layer = layui.layer
        util = layui.util
        select = layui.customSelect
        $ = layui.$
        form = layui.form



        load_tree_data()

        form.on('submit(btn_save)', function(obj){

            save(obj.field)

        });
    });
</script>
</body>
</html>